﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabTurtorial
{
    public partial class Lab07DGI : Form
    {
        Graphics graphics;
        ColorDialog cdlgFastPaint;
        Pen pen;
        SolidBrush brush;
        Color penColor, brushColor;
        string graphicsObject, drawOrFill;
        int lineXOne, lineYOne, lineXTwo, lineYTwo;

        public Lab07DGI()
        {
            InitializeComponent();
        }

        private void Lab07DGI_Load(object sender, EventArgs e)
        {
            penColor = Color.Blue;
            brushColor = Color.Blue;
            ResetVariableControls();
            this.Location = new Point(1,1);
        }
        private void ResetVariableControls() {
            CreateGraphicsAndPen();
            drawOrFill = "";
            lblControls.Cursor = null;
            btnCircle.Enabled = true;
            btnClear.Enabled = false;
            btnColor.Enabled = false;
            btnFill.Enabled = false;
            btnLine.Enabled = true;
            btnRectangle.Enabled = true;
        }
        private void CreateGraphicsAndPen() {
            graphics = lblControls.CreateGraphics();
            graphics.Clear(Color.White);
            pen = new Pen(penColor);
            brush = new SolidBrush(brushColor);
        }
        // enabled the controls
        private void EnabledObjectButtons() {
            btnLine.Enabled = true;
            btnCircle.Enabled = true;
            btnRectangle.Enabled = true;
        }
        //enabled the controls
        private void EnabledEffectButtons() {
            btnFill.Enabled = true;
            btnClear.Enabled = true;
            btnColor.Enabled = true;
        }
        //Draws a line
        private void btnLine_Click(object sender, EventArgs e)
        {
            graphicsObject = "Line";
            drawOrFill = "Draw";
            CreateGraphicsAndPen();
            lineXOne = 0;
            lineYOne = 0;
            lblControls.Cursor = new Cursor(@"C:\WINDOWS\Cursors\cross_i.cur");
            btnFill.Enabled = false;
            btnClear.Enabled = false;
            EnabledObjectButtons();
            btnLine.Enabled = false;
            btnColor.Enabled = true;
        }
        //Draws a circle
        private void btnCircle_Click(object sender, EventArgs e)
        {
            graphicsObject = "Circle";
            drawOrFill = "Draw";
            CreateGraphicsAndPen();
            graphics.DrawEllipse(pen,40,50,150,150);
            btnFill.Enabled = true;
            EnabledObjectButtons();
            btnCircle.Enabled = false;
            lblControls.Cursor = null;
            EnabledEffectButtons();
        }
        //Draws a rectangle
        private void btnRectangle_Click(object sender, EventArgs e)
        {
            graphicsObject = "Rectangle";
            drawOrFill = "Draw";
            CreateGraphicsAndPen();
            graphics.DrawRectangle(pen, 40, 50, 150, 150);
            btnFill.Enabled = true;
            EnabledObjectButtons();
            btnRectangle.Enabled = false;
            lblControls.Cursor = null;
            EnabledEffectButtons();
        }
        //Fill the graphics Object.
        private void btnFill_Click(object sender, EventArgs e)
        {
            drawOrFill = "Fill";
            DrawFillObject();
            btnFill.Enabled = false;
            btnClear.Enabled = true;
        }
        //Draws or Fills the objects.
        private void DrawFillObject() {
            if (graphicsObject != "Line") {
                lblControls.Refresh();
                CreateGraphicsAndPen();
            }
            if (drawOrFill == "Fill")
            {
                if (graphicsObject == "Circle")
                {
                    graphics.FillEllipse(brush, 40, 50, 150, 150);
                }
                else if (graphicsObject == "Rectangle")
                {
                    graphics.FillRectangle(brush, 40, 50, 150, 150);
                }
            }
            else  if(drawOrFill == "Draw"){
                if (graphicsObject == "Circle")
                {
                    graphics.DrawEllipse(pen, 40, 50, 150, 150);
                }
                else if (graphicsObject == "Rectangle")
                {
                    graphics.DrawRectangle(pen, 40, 50, 150, 150);
                }
            }
        }

        //Draws the graphics object
        private void btnClear_Click(object sender, EventArgs e)
        {
            ResetVariableControls();
        }
        //open color dialog box
        private void btnColor_Click(object sender, EventArgs e)
        {
            cdlgFastPaint = new ColorDialog();
            if (cdlgFastPaint.ShowDialog() == DialogResult.OK) {
                penColor = cdlgFastPaint.Color;
                pen = new Pen(cdlgFastPaint.Color);
                brushColor = cdlgFastPaint.Color;
                brush = new SolidBrush(cdlgFastPaint.Color);
            }
            DrawFillObject();
        }

        private void lblControls_MouseClick(object sender, MouseEventArgs e)
        {
            if ( graphicsObject == "Line") {
                if (lineXOne == 0 && lineYOne == 0)
                {
                    lineXOne = e.X;
                    lineYOne = e.Y;
                }
                else {
                    lineXTwo = e.X;
                    lineYTwo = e.Y;
                    graphics.DrawLine(pen,lineXOne,lineYOne,lineXTwo,lineYTwo);
                    lineXOne = 0;
                    lineYOne = 0;
                    btnClear.Enabled = true;
                }
            }
        }



    }
}
